package cn.dengta.webapp.user.entity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.Objects;

import cn.dengta.common.model.*;
import cn.dengta.webapp.base.entity.BizEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.Cmp;
import me.codeplayer.util.X;

/**
 * 渠道成本
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("dt_channel_cost")
public class ChannelCost extends BizEntity {

	/** 日期 */
	protected Date date;
	/** 渠道 */
	protected Long channelId;
	/** 渠道成本 */
	private BigDecimal cost;
	/** 对应人民币渠道成本 */
	private BigDecimal cnyCost;
	/** 注册人数 */
	public Integer registerCount;

	public ChannelCost init(ChannelCost input, Date now) {
		this.channelId = X.expectNotNull(input.channelId, channelId);
		this.cost = X.expectNotNull(input.cost, cost);
		this.cnyCost = X.expectNotNull(input.cnyCost, cnyCost);
		this.date = X.expectNotNull(input.date, date);
		this.registerCount = X.expectNotNull(input.registerCount, registerCount);
		this.status = X.expectNotNull(this.status, Status.WAIT_SUBMIT.value);
		this.state = X.expectNotNull(this.state, State.PUBLIC);
		super.initTime(now);
		return this;
	}

	public ChannelCost init(Date date, Long channelId, BigDecimal cost, Integer registerCount, Date now) {
		this.date = date;
		this.channelId = channelId;
		this.cost = cost;
		this.registerCount = registerCount;
		this.status = X.expectNotNull(this.status, Status.WAIT_SUBMIT.value);
		this.state = X.expectNotNull(this.state, State.PUBLIC);
		super.initTime(now);
		return this;
	}

	public BigDecimal getRegisterCost() {
		return Cmp.nullToZero(registerCount) == 0 ? BigDecimal.ZERO : cost.divide(BigDecimal.valueOf(registerCount), 2, RoundingMode.HALF_DOWN);
	}

	public static final String DATE = "date";
	public static final String CHANNEL_ID = "channel_id";
	public static final String COST = "cost";


	public Status status() {
		return Status.of(status);
	}

	@Getter
	public enum Status implements ValueProxyImpl<Status, Integer> {
		/** 财务复审不通过 */
		NOT_PASS_REVIEW(-2, "财务复审不通过", true),
		/** 财务审核不通过 */
		NOT_PASS_AUDIT(-1, "财务审核不通过", true),
		/** 待提交 */
		WAIT_SUBMIT(0, "待提交", true),
		/** 待财务审核 */
		WAIT_AUDIT(1, "待财务审核", false),
		/** 财务审核已完成 */
		AUDIT_COMPLETE(2, "财务审核已完成", false);

		public final Integer value;
		private final String label;
		private final boolean isEdit;
		private final ValueProxy<Status, Integer> proxy;

		Status(int value, String label, boolean isEdit) {
			this.value = value;
			this.label = label;
			this.isEdit = isEdit;
			proxy = new ValueProxy<>(this, value, label);
		}

		public boolean eq(Integer value) {
			return Objects.equals(this.value, value);

		}

		public static Status of(Integer value) {
			for (Status status : Status.values()) {
				if (Objects.equals(status.value, value)) {
					return status;
				}
			}
			return null;
		}

	}

}
